/**
 * 
 */
package model.container;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;

import model.Lector;
import persistenty.ContainerTekstPersisteerbaar;

/**
 * This class is intended to setup the Lectoren container object.
 * 
 * @author Nicolas Vanderhulst
 * @version 20111009
 * 
 */
public class Lectoren extends ContainerTekstPersisteerbaar implements Comparable<Lectoren>, Iterable<Lector>, Cloneable {
	public HashSet<Lector> lectoren;
	private Cursussen cursussen;
	private int lectorID = 0;

	/**
	 * @return the lectorID
	 */
	public int getLectorID() {
		return lectorID;
	}

	/**
	 * @param lectorID
	 *            the lectorID to set
	 */
	public void setLectorID(int lectorID) {
		this.lectorID = lectorID;
	}

	public Lectoren() throws LectorenException {
		lectoren = new HashSet<Lector>();
		persistentyFiles = new ArrayList<String>();
		persistentyFiles.add("src/persitentyfiles/Lector.txt");
	}

	public void add(Lector c) throws CloneNotSupportedException, LectorenException {
		if (c.getLectorCode() <= this.lectorID) {
			c.setLectorCode(lectorID++);
		}
		if (!lectoren.add(c)) {
			throw new LectorenException("Deze lectorcode bestaat reeds");
		}
		try {
			BufferedWriter writer = new BufferedWriter(new FileWriter(String.format("src/persitentyfiles/%s.txt", c
					.getClass().getSimpleName()), true));
			writer.append(c.maakTekstVanObject());
			writer.close();
		} catch (IOException ioException) {
			ioException.printStackTrace();
		}
	}

	public void remove(Lector c) {
		lectoren.remove(c);
	}

	public boolean checkLectorCodeExists(Lector a) throws CloneNotSupportedException {
		boolean b = false;
		for (Lector c : lectoren) {
			if (c.getLectorCode() == a.getLectorCode()) {
				b = true;
			}
		}
		return b;
	}

	public String[] getLectoren() {
		int i = 0;
		String[] lectorenAlsString = new String[lectoren.size()];
		Iterator<Lector> it = lectoren.iterator();
		while (it.hasNext()) {
			Lector c = it.next();
			lectorenAlsString[i] = c.getLectorNaam();
			i++;
		}
		return lectorenAlsString;

	}

	/**
	 * Geeft een string van lectoren weer volgens hun cursusvoorkeur
	 */
	public StringBuffer getLectorByPreferredCourse(String cursusNaam) {
		int lectorCount = 0;
		StringBuffer lectorenAlsString = new StringBuffer();
		for (Lector l : this.lectoren) {
			if (l.getCursusVoorkeur() != null) {
				if (l.getCursusVoorkeur().getCursusNaam().equalsIgnoreCase(cursusNaam)) {
					lectorenAlsString.append("ID: " + l.getID() + "\t");
					lectorenAlsString.append("Naam: " + l.getLectorNaam() + "\n");
					lectorCount++;
				}
			}
		}
		if (lectorCount == 0) {
			lectorenAlsString = null;
			lectorenAlsString = new StringBuffer();
			lectorenAlsString.append("Geen lectoren met als voorkeurvak: " + cursusNaam);
		}
		return lectorenAlsString;
	}

	/**
	 * Geeft een lector terug volgens zijn code
	 */
	public Lector getLectorByCode(int lectorCode) {
		Lector tempLector = null;
		for (Lector l : this.lectoren) {
			if (l.getLectorCode() == lectorCode) {
				tempLector = l.clone();
			}
		}
		return tempLector;
	}

	@Override
	public Iterator<Lector> iterator() {
		return lectoren.iterator();
	}

	@Override
	public int compareTo(Lectoren arg0) {
		return this.toString().compareTo(arg0.toString());
	}

	@Override
	public void schrijven() throws IOException {
		try {
			BufferedWriter writer = new BufferedWriter(new FileWriter("src/persitentyfiles/Lector.txt", false));
			for (Lector l : lectoren) {
				writer.write(l.maakTekstVanObject());
			}
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((lectoren == null) ? 0 : lectoren.hashCode());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof Lectoren)) {
			return false;
		}
		Lectoren other = (Lectoren) obj;
		if (lectoren == null) {
			if (other.lectoren != null) {
				return false;
			}
		} else if (!lectoren.equals(other.lectoren)) {
			return false;
		}
		return true;
	}

	/**
	 * @param lectoren
	 *            the lectoren to set
	 */
	public void setLectoren(HashSet<Lector> lectoren) {
		this.lectoren = lectoren;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Lectoren [lectoren=" + lectoren + "]";
	}

	/**
	 * @return Lector
	 * @see java.util.HashSet#clone()
	 */
	@Override
	public Object clone() {
		return lectoren.clone();
	}

	public void setBeschikbareCursussen(Cursussen cursusContainer) {
		this.cursussen = cursusContainer;
	}

	@Override
	public void createObject() throws CursussenException, CloneNotSupportedException, InschrijvingenException,
			LectorenException, Exception {
		try {
			Lector lector = null;
			if (inputFile.equalsIgnoreCase("src/persitentyfiles/Lector.txt")) {
				lector = new Lector();
			}
			if (lector != null) {
				lector = lector.maakObjectVanTekst(objectAlsTekst);
				lector.invullenCursusvoorkeurVanTekst(objectAlsTekst, cursussen);
				lectoren.add(lector);
				lectorID = (lectorID < lector.getID() ? lector.getID() : lectorID);
			}
		} catch (LectorenException evenementException) {
			evenementException.printStackTrace();
		} catch (NumberFormatException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
